package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.DeliverGoods;

/**
 * Created by kylin on 2016/9/19.
 */
public class DeliverGoodAdapter extends RecyclerView.Adapter{


    private static final String TAG ="DataAdapter" ;
    private LayoutInflater mLayoutInflater;
    private SortedList<DeliverGoods.DataEntity> mSortedList;
    private Context context;
    List<Boolean> isflag = new ArrayList<>();
    public OnLineClickListener onLineClickListener;


    public interface OnLineClickListener{
        void onClick(DeliverGoods.DataEntity item);
    }

    public void setOnLineClickListener(OnLineClickListener onLineClickListener) {
        this.onLineClickListener = onLineClickListener;
    }

    public DeliverGoodAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);

//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(DeliverGoods.DataEntity.class, new SortedList.Callback<DeliverGoods.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(DeliverGoods.DataEntity o1, DeliverGoods.DataEntity o2) {

                if (Integer.parseInt(o1.getRownumber())<Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber())>Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(DeliverGoods.DataEntity oldItem, DeliverGoods.DataEntity newItem) {
                return oldItem.getRownumber().equals(oldItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(DeliverGoods.DataEntity item1, DeliverGoods.DataEntity item2) {
                boolean statue  ;
                if (item1.getId() == item2.getId() &&
                        item1.getCustorderid().equals(item2.getCustorderid())&&
                item1.getCustName().equals(item2.getCustName())){
                        statue=true;
                }else {
                    statue=false;
                }

                return statue;
            }

            @Override
            public void onInserted(int position, int count) {

                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<DeliverGoods.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for(DeliverGoods.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
            isflag.add(false);
        }
        mSortedList.endBatchedUpdates();
    }
    public void addContext(Context context){
        this.context=context;
    }

    public void deleteItems(ArrayList<DeliverGoods.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (DeliverGoods.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new mViewHolder(mLayoutInflater.inflate(R.layout.item_base_no_audit, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        Log.d(TAG, "DataAdapter:4");
        final DeliverGoods.DataEntity item = mSortedList.get(position);
        mViewHolder viewHolder = (mViewHolder) holder;
        viewHolder.textView.setText(item.getRownumber()+"");
        if (isflag.get(position)){
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        }else {
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.blue_shallow));
        }
        String cN;
        if (item.getCustSName()!=null){
             cN = item.getCustSName();
        }else{
            cN =item.getCustName();
        }

//        viewHolder.tv_title.setText("客户单号:\t"+item.getCustorderid() + "\n"+"客户名称:\t"+cN
//                + "\n"+"订单日期:\t" +item.getSenddate() +
//                "\n"+"产品名称:\t"+item.getProdName() + "\n"+"订单数量:\t" + item.getPricenumb() +
//                "\n"+"送货日期:\t"+ item.getSenddate() + "\n"+"送货数:\t\t\t"+item.getPaynumb() +

//        );
        viewHolder.tv_title.setText("客户单号:\t"+item.getCustorderid() + "\n"+"客户名称:\t"+cN);
        viewHolder.lineItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLineClickListener!=null){
                    int size = isflag.size();
                    for (int i = 0; i < size; i++) {
                        isflag.set(i, false);
                    }
                    isflag.set(position, true);
                    notifyDataSetChanged();
                    onLineClickListener.onClick(item);
                }
            }
        });
    }

    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

     static  class mViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;
        private CardView cardView;
        private LinearLayout lineItem;
        public mViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
                cardView = (CardView) itemView.findViewById(R.id.card);

            textView = (TextView) itemView.findViewById(R.id.tv_id);
             tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            lineItem = (LinearLayout)itemView.findViewById(R.id.lineItem);
        }
    }
}
